

//
//  GuideViewController.m
//  Tospur-text
//
//  Created by  grogue on 15/10/8.
//  Copyright © 2015年 Tospur_hewei. All rights reserved.
//

#import "GuideViewController.h"
#import "LoginViewController.h"
#import <SDCycleScrollView.h>
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import "Encryption.h"
#import "NetWork.h"
@interface GuideViewController ()<SDCycleScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *guideScrollView;
@property(nonatomic,strong) UIPageControl *guidePageControl;
@property (nonatomic,strong) NSArray *imageArray;
@property (nonatomic,strong) NSMutableArray *imageUrlArray;
@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cycleScrollView];
   // [self requestGuideImage];
    // Do any additional setup after loading the view.
}
- (void)requestGuideImage{
    NSArray *urlArray;
    //urlArray = request.....
    NSInteger count = urlArray.count;
    self.guideScrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.guideScrollView.pagingEnabled = YES;
    self.guideScrollView.bounces = NO;
    self.guideScrollView.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width*count, [[UIScreen mainScreen] bounds].size.height);
    self.guidePageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width-100)/2.0,
                                                                              [[UIScreen mainScreen] bounds].size.height-60,
                                                                              100,
                                                                              40)];
    self.guidePageControl.numberOfPages = count;
    for (int i = 0; i < count; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width*i,
                                                                             0,
                                                                             [[UIScreen mainScreen] bounds].size.width,
                                                                              [[UIScreen mainScreen] bounds].size.height)];
        [self.guideScrollView addSubview:imageView];
        [imageView sd_setImageWithURL:urlArray[i] placeholderImage:[UIImage imageNamed:@"楼.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if(i == count - 1){
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width*i+([[UIScreen mainScreen] bounds].size.width - 100)/2, [[UIScreen mainScreen] bounds].size.height - 80, 100, 50);
                [button setTitle:@"开始使用" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(startApp:) forControlEvents:UIControlEventTouchUpInside];
                [self.guideScrollView addSubview:button];
                [self.view addSubview:self.guideScrollView];
                [self.view addSubview:self.guidePageControl];
            }
        }];
    }
}
- (void)startApp:(id)sender{
    [[NSUserDefaults standardUserDefaults]setObject:@"manualLogin" forKey:@"loginFlag"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self presentViewController:loginVC animated:YES completion:nil];
}

- (void)postImageUrl
{
    AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    tx.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    tx.requestSerializer = [AFJSONRequestSerializer serializer];
    // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSString *urlStr = @"AAAhttp://172.16.16.83:7001/api/GetOnloadImgA";
    NSString *signature = [[Encryption shareInstance]sha1];
    NSLog(@"+++++ %@",signature);
    NSDictionary *dict = @{
                           @"appkey": @"tospur123",
                           @"signature": signature,
                           };
    [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray *array = [responseObject objectForKey:@"ImaList"];
        self.imageUrlArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            NSString *str = [dic objectForKey:@"ImgUrl"];
            
            [self.imageUrlArray addObject:str];
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

- (SDCycleScrollView*)cycleScrollView
{
    //self.imageArray = @[[ UIImage imageNamed:@"方向.jpg"],[ UIImage imageNamed:@"风景.jpg"],[ UIImage imageNamed:@"努力.jpg"],[ UIImage imageNamed:@"坚持.jpg"],[ UIImage imageNamed:@"认真.jpg"]];
    
    AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    tx.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    tx.requestSerializer = [AFJSONRequestSerializer serializer];
    // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSString *urlStr = @"http://172.20.5.142:7001/api/GetOnloadImg";
    NSString *signature = [[Encryption shareInstance]sha1];
    NSLog(@"+++++ %@",signature);
    NSDictionary *dict = @{
                           @"appkey": @"tospur123",
                           @"signature": signature,
                           //@"sessionid": self.sessionid,
                           @"version": @"1.0",
                           };
    [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray *array = [responseObject objectForKey:@"ImgList"];
        self.imageUrlArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            NSString *str = [dic objectForKey:@"ImgUrl"];
            
            [self.imageUrlArray addObject:str];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:[[UIScreen mainScreen] bounds] imagesGroup:_imageUrlArray];
    cycleScrollView.dotColor = [UIColor redColor];
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.delegate = self;
    [self.view addSubview:cycleScrollView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // cycleScrollView.titlesGroup = _titleArray;
        cycleScrollView.imageURLStringsGroup = _imageUrlArray;
        cycleScrollView.contentMode = UIViewContentModeTop;
        cycleScrollView.clipsToBounds = YES;
        cycleScrollView.layer.shadowColor = [UIColor blackColor].CGColor;
        cycleScrollView.layer.shadowOpacity = 0.6;
        cycleScrollView.layer.shadowRadius = 1.0;
        cycleScrollView.clipsToBounds = NO;
    });
    [self.view addSubview:cycleScrollView];
    
    return nil;
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    if(index == self.imageUrlArray.count - 1){
        [[NSUserDefaults standardUserDefaults]setObject:@"manualLogin" forKey:@"loginFlag"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self presentViewController:loginVC animated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
